package com.rnd.kx.mvp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rnd.kx.mvp.R;
import com.rnd.kx.mvp.bean.IndexBean;
import com.rnd.kx.mvp.utils.RangleTransForm;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexRecipeAdapter extends RecyclerView.Adapter<IndexRecipeAdapter.ViewHolder> implements View
        .OnClickListener {
    private List<IndexBean.CatesBean.RecipeBean> list;
    private Context context;

    public OnItemClickListener mOnItemClickListener;

    public IndexRecipeAdapter(List<IndexBean.CatesBean.RecipeBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public IndexRecipeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_index_recipe, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(IndexRecipeAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        IndexBean.CatesBean.RecipeBean recipeBean = list.get(position);
        if (recipeBean.getCover() != null && !TextUtils.isEmpty(recipeBean.getCover())){
            Picasso.with(context)
                    .load(recipeBean.getCover())
                    .transform(new RangleTransForm(40))
                    //.error(R.drawable.img_default)
                    .into(holder.recipe_img);
        }

        if (recipeBean.getTitle() != null && !TextUtils.isEmpty(recipeBean.getTitle())){
            if (recipeBean.getTitle().length() > 6){
                holder.recipe_name.setText(recipeBean.getTitle().substring(0,6)+"...");
            }else {
                holder.recipe_name.setText(recipeBean.getTitle());
            }

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (mOnItemClickListener != null){
            switch (v.getId()){
                default:
                    mOnItemClickListener.onClick(v, ViewName.ITEM,position);
                    break;
            }
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_index_recipe_img)
        ImageView recipe_img;
        @BindView(R.id.item_index_recipe_name)
        TextView recipe_name;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(IndexRecipeAdapter.this);
        }
    }

    public interface OnItemClickListener {
        void onClick(View view, ViewName viewName, int position);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;
    }

    public enum ViewName{
        ITEM

    }
}
